package cn.stylefeng.guns.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.*;
import cn.stylefeng.guns.modular.system.mapper.ColorMapper;
import cn.stylefeng.guns.modular.system.mapper.ProductOrderMapper;
import cn.stylefeng.guns.modular.system.model.Wrapper.ProductOrderWrapper;
import cn.stylefeng.guns.modular.system.model.params.ProductOrderParam;
import cn.stylefeng.guns.modular.system.model.result.resultVO.ProductDetailList;
import cn.stylefeng.guns.modular.system.model.result.resultVO.ProductOrderDetailVO;
import cn.stylefeng.guns.modular.system.model.result.resultVO.SalesProductVO;
import cn.stylefeng.guns.modular.system.service.*;
import cn.stylefeng.guns.modular.system.service.impl.ProductModelServiceImpl;
import cn.stylefeng.guns.modular.system.service.impl.ProductOrderServiceImpl;
import cn.stylefeng.guns.sys.core.constant.cache.Cache;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.core.util.common.*;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.RenderUtil;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import org.apache.http.entity.ContentType;
import org.hibernate.validator.constraints.URL;
import org.quartz.SimpleTrigger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.util.annotation.Nullable;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;


/**
 * 产品编号表控制器
 *
 * @author 小飞
 * @Date 2019-06-27 19:55:45
 */
@Controller
@RequestMapping("/productOrder")
public class ProductOrderController extends BaseController {

    private String PREFIX = "/productOrder";
    private static  final Joiner JOINER =Joiner.on("/").skipNulls();
    private static  final Splitter SPLITTER = Splitter.on(",").trimResults().omitEmptyStrings();

    @Autowired
    private PackageSizeService packageSizeService;


    @Autowired
    private ProductOrderService productOrderService;
    @Autowired
    private ClassificationService classificationService;

    @Autowired
    private ProductOrderMapper productOrderMapper;

    @Autowired
    private UnitService unitService;

    @Autowired
    private ChannelService channelService;

    @Autowired
    private SizeService sizeService;

    @Autowired
    private ColorService colorService;


    /**
     * 跳转到主页面
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/productOrder.html";
    }

    /**
     * 新增页面
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/add")
    public String add() {
        //单位分类
        super.setAttr("unit",unitService.list());
        super.setAttr("size",sizeService.list());

        return PREFIX + "/productOrder_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/productOrder_edit.html";
    }


    /**
     * 获取颜色详情
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/colorDetail/{id}")
    public ResponseData getColorDetail(@PathVariable  Long id){
        String nameOne = colorService.getById(id).getName();
        String nameTwo = colorService.getById(colorService.getById(id).getPid()).getName();
        String nameThree = null;
        if (colorService.getById(id).getPid() != 0L){
            nameThree = colorService.getById(colorService.getById(colorService.getById(id).getPid()).getPid()).getName();
        }
        return  ResponseData.success(200,"查询成功",JOINER.join(Lists.newArrayList(nameThree,nameTwo,nameOne)));
    }
    /**
     * 新增接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ProductOrderParam productOrderParam) {


        ProductOrder productOrder = new ProductOrder();
        BeanUtils.copyProperties(productOrderParam,productOrder);
        productOrder.setCreateUser(ShiroKit.getUserNotNull().getName());
        productOrder.setCreateTime(new Date());
        //插入分类表

        Classification classification = new Classification();
        classification.setName(productOrderParam.getProductOrderName());
        classification.setPid(productOrderParam.getKindId());
        classificationService.save(classification);
        //找出刚插入的数据 的id
        Classification classificationLast = classificationService.getOne(new QueryWrapper<Classification>()
        .orderByDesc("id").last("limit 1"));
        productOrder.setKindId(classificationLast.getId());
        productOrderService.save(productOrder);
        return ResponseData.success();
    }
    /**
     * 编辑接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ProductOrderParam productOrderParam) {
        this.productOrderService.update(productOrderParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ProductOrderParam productOrderParam) {

        //删除分类
        ProductOrder productOrder = productOrderService.getById(productOrderParam.getId());
        classificationService.getBaseMapper().deleteById(productOrder.getKindId());

        //删除表
        this.productOrderService.delete(productOrderParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ProductOrderParam productOrderParam) {
        ProductOrder detail = this.productOrderService.getById(productOrderParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 小飞
     * @Date 2019-06-27
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ProductOrderParam productOrderParam) {
        return this.productOrderService.findPageBySpec(productOrderParam);
    }


    @ResponseBody
    @RequestMapping("/productDetailList")
    public LayuiPageInfo productDetailList(){
        Page<ProductDetailList> page;
        Page pageContext = LayuiPageFactory.defaultPage();
        page = productOrderMapper.IndentList(pageContext,ShiroKit.getUserNotNull().getId());
            return LayuiPageFactory.createPageInfo(page);

    }

    @ResponseBody
    @RequestMapping("/salsOrderList")
    public  LayuiPageInfo salsOrderList(){
        IPage<List<SalesProductVO>> page =  productOrderMapper.salsList(LayuiPageFactory.defaultPage());
        return  LayuiPageFactory.createPageInfo(page);
    }


    @RequestMapping("/upload")
    public ResponseEntity<?> upload(MultipartFile multipartFile){
        ResponseMsg msg = new ResponseMsg();
        msg = UploadFileUtils.uploadFile(multipartFile);
        if (msg.getStatus() == 10001){
            UploadResult result = (UploadResult) msg.getResult();
            msg.setResponseSuccess("上传成功",result.getPath());
            return  new ResponseEntity<>(msg, HttpStatus.OK);
        }else {
            msg.setResponseError(SysErrorCode.UPLOAD_ERROR);
            return  new ResponseEntity<>(msg,HttpStatus.OK);
        }
    }


    /**
     * 跳转详情
     */
    @RequestMapping("/shiIndentImgeEdit")
    public String shiIndentImgeEdit(Long id) {
        super.setAttr("id",id);
        return PREFIX + "/shi_Indent_Imge_edit.html";
    }









}


